package ar.edu.unq.tpi.labso.rochi.agendaRochi.nuevosGenericos;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

import model.Aplicacion.Agenda;

import org.apache.wicket.markup.repeater.data.IDataProvider;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.IModel;
import org.uqbar.commons.model.CollectionBasedHome;
import org.uqbar.commons.model.Entity;

import clasesPrincipales.Contacto;

public abstract class DatProvider<T extends Entity> implements IDataProvider<T>{

	protected static final long serialVersionUID = 1L;
	protected List< T >guardador;
	protected T seleccionado;


	public void setSeleccionado(T modelObject) {
		this.seleccionado = modelObject;		
	}
	
	public T getSeleccionado() {
		return  this.seleccionado;
	}
	
	public abstract Class < T > clase() ;
	
	public void buscar(T example){
		guardador= ((CollectionBasedHome<T>)Agenda.getHome(this.clase())).buscarByExample(example);
	}
	public void agregar(T example){
		((CollectionBasedHome<T>)Agenda.getHome(this.clase())).agregar(example);
		guardador= ((CollectionBasedHome<T>)Agenda.getHome(Contacto.class)).buscarTodos();
	}
	public void editar(T example) {
		((CollectionBasedHome<T>)Agenda.getHome(this.clase())).actualizar(example);
		
		guardador= ((CollectionBasedHome<T>)Agenda.getHome(this.clase())).buscarTodos();
	
	}
	
	
	
	
	
	public Iterator<T> iterator(int arg0, int arg1) {
		/* Lista de resultados */
		List<T>resultados = new LinkedList<T>();
		for(int i= arg0 ; i<arg1; i++){
			resultados.add(guardador.get(i));
		}
		return resultados.iterator();
	}

	public IModel<T> model(T arg0) {
		// TODO Auto-generated method stub
		return  new CompoundPropertyModel<T>(arg0);

	}

	public int size() {
		return guardador.size();
	}

	public void detach() {		
	}
}
